#include <cstddef>
class Solution
{
public:
    ListNode* ReLt(ListNode* head)
    {
        if (head == NULL || head->next == NULL)  return head;

        else
        {
            ListNode* newhead = ReLt(head->next);
            head->next->next = head;
            head->next = NULL;
            return newhead;
        }
    }
    ListNode* addInList(ListNode* head1, ListNode* head2)
    {
        if (head1 == NULL) return head2;
        if (head2 == NULL) return head1;

        head1 = ReLt(head1);
        head2 = ReLt(head2);

        ListNode* ans = new ListNode(0);
        ListNode* cur = ans;
        int r = 0;

        while (head1 || head2 || r)
        {
            int val1 = head1 ? head1->val : 0;
            int val2 = head2 ? head2->val : 0;
            int sum = val1 + val2 + r;
            r = sum / 10;
            sum %= 10;
            cur->next = new ListNode(sum);
            cur = cur->next;

            if (head1) head1 = head1->next;
            if (head2) head2 = head2->next;
        }

        return ReLt(ans->next);
    }
};